﻿using HenuAlbum.DbContext;
using HenuAlbum.Identity;
using HenuAlbum.Models;
using HenuAlbum.ResponseModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HenuAlbum.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class NotificationController : ControllerBase
    {
        private readonly AppDbContext _appDbContext;
        private readonly UserManager<User> _userManager;

        public NotificationController(AppDbContext appDbContext, UserManager<User> userManager)
        {
            _appDbContext = appDbContext;
            _userManager = userManager;
        }
        [HttpGet("GetAlertNotices")]
        [Authorize]
        public async Task<IActionResult> GetAlertNotifications()
        {
            var alert =await _appDbContext.Notifications.Include(e=>e.Creator).Where(x => x.IsAlert).FirstOrDefaultAsync();
            if (null == alert) return BadRequest(new IncorrectError("管理员未设置公告！"));
            return Ok(new OkResponse() { Data =new
            {
                alert.Id,
                alert.Title,
                alert.Content,
                alert.CreateTime,
                alert.IsAlert,
                CreatorName = alert.Creator.UserName,
            }
            });
        }

        [HttpGet("GetNotices")]
        [Authorize]
        public async Task<IActionResult> GetCommonNotifications()
        {
            var notices = await _appDbContext.Notifications.Include(x => x.Creator)
                                                            .Where(y=>!y.IsAlert)
                                                            .Select(e => new
                                                            {
                                                                e.Id,
                                                                e.Title,
                                                                e.Content,
                                                                e.CreateTime,
                                                                CreatorName = e.Creator.UserName,
                                                            })
                                                            .ToArrayAsync();
            return Ok(new OkResponse() { Data = notices });
        }
        [HttpPost("AddNotice")]
        [Authorize(Roles = "管理员")]
        public async Task<IActionResult> AddNotice(NotificationOperator param)
        {
            //获取创建通知的管理员的身份信息信息
            var user = await _userManager.GetUserAsync(this.User);
            if(param.IsAlert)
            {
                var alert = await _appDbContext.Notifications.Where(x => x.IsAlert).ToArrayAsync();
                _appDbContext.Notifications.RemoveRange(alert);
                await _appDbContext.SaveChangesAsync();
            }
            Notification notification = new Notification()
            {
                Title = param.Title,
                Content = param.Content,
                IsAlert = param.IsAlert,
                CreatorId = user.Id,
                CreateTime = System.DateTime.Now,
                Creator = user
            };
            

            await _appDbContext.Notifications.AddAsync(notification);
            await _appDbContext.SaveChangesAsync();
            return Ok(new OkResponse());
        }

        [HttpPost("{id:int}/DeleteNotice")]
        [Authorize(Roles = "管理员")]
        public async Task<IActionResult> Delete(int id)
        {
            var notice = await _appDbContext.Notifications.Where(x => x.Id == id).FirstOrDefaultAsync();
            if (null == notice) return BadRequest(new NotExistsError("公告不存在"));
            _appDbContext.Notifications.Remove(notice);
            await _appDbContext.SaveChangesAsync();
            return Ok(new OkResponse());
        }

        [HttpPost("{id:int}/UpdateNotice")]
        [Authorize(Roles = "管理员")]
        public async Task<IActionResult> Update(int id, NotificationOperator param)
        {
            var notice = await _appDbContext.Notifications.Where(x => x.Id == id).FirstOrDefaultAsync();
            if (null == notice) return BadRequest(new NotExistsError("公告不存在"));
            if(!notice.IsAlert&&param.IsAlert)
            {
                var alert = await _appDbContext.Notifications.Where(x => x.IsAlert).ToArrayAsync();
                _appDbContext.Notifications.RemoveRange(alert);
                await _appDbContext.SaveChangesAsync();
            }
            notice.Title = param.Title;
            notice.Content = param.Content;
            notice.IsAlert = param.IsAlert;

            await _appDbContext.SaveChangesAsync();
            return Ok(new OkResponse());
        }
    }
}
